"""
@author: ZY
@Time: 2021/10/07
"""

from Script.swggerApi import SwaggerApi
import ast


class Body:
    """
    封装请求体
    """

    def body(self, uri):
        """
        根据uri获取请求体
        """
        resp = SwaggerApi.swagger()
        # 1. 根据uri获取到 definitions 的json的key 暂时只测post请求的接口
        if 'post' in resp['paths'][uri]:
            data = resp['paths'][uri]['post']['parameters'][0]['schema']['$ref']
            # 2. 分割取到json的key
            data = data[14:]
            # 3. 取到消息体
            params = resp['definitions'][data]['properties']
            dic = ''
            for i in params:
                if 'example' in params[i].keys():
                    params[i]['example']
                    if isinstance(params[i]['example'], list):
                        dic += '"{0}":{1},'.format(i, params[i]['example'])
                    else:
                        dic += '"{0}":"{1}",'.format(i, params[i]['example'])
                else:
                    pass
            return ast.literal_eval('{' + dic + '}')
        else:
            return ast.literal_eval('{}')


Body = Body()

if __name__ == '__main__':
    test_uri = '/jpa/sysUser/insert'
    result = Body.body(test_uri)
    print(result)
    print(type(result))
